package cn.edu.zut.cs.Business;

import cn.edu.zut.cs.DAO.StudentDAO;
import cn.edu.zut.cs.Model.Student;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.awt.print.Book;
import java.util.List;

@Service
public class SaleService {

    @Autowired
    StudentDAO studentDAO;

    public void insert(Student student) {
        studentDAO.insertStudent(student);
    }

    public List<Student> qureyAll() {
        return studentDAO.qureyAll();
    }

    public Student getStudentById(int id){
        return studentDAO.getStudentById(id);
    }

    public int insertStudent(Student entity) throws Exception {
        if(entity.getName()==null||entity.getName().equals("")){
            throw new Exception("姓名必须不为空");
        }
        return studentDAO.insertStudent(entity);
    }

    @Transactional
    public int insertStudent(Student entity1,Student entityBak){
        int rows=0;
        rows=studentDAO.insertStudent(entity1);
        rows=studentDAO.insertStudent(entityBak);
        return rows;
    }

    public int deleteStudent(int id) {
        return studentDAO.deleteStudent(id);
    }
    public int deleteStudent(String[] ids){
        int rows=0;
        for (String idStr : ids) {
            int id=Integer.parseInt(idStr);
            rows+=deleteStudent(id);
        }
        return rows;
    }

    public int updateStudent(Student entity) {
        return studentDAO.updateStudent(entity);
    }


}
